/**
 * 回款信息维护 - 编辑页面
 * @author mengyb
 * @date 2019-08-09
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        layui=win.layui,
        table,
        vueData = {
            isAdd: false,
            isDetail:false,
            form: '',
            mx:'',
            gweo003:1,//汇率
            digit: {
                rate: 100,
                price: 100,
                amount: 100
            },
            rules: {
                GWEO002: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWEODOCNO: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWEODOCDATE: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ]
            },
            selectOptions:{
            }
        },
        cols=[[
            {type: 'checkbox', title: '选择', fixed: 'left'}
            ,{field:'GWEOSEQ',width:60,title:'序号'}
            , {field: 'GWEO001', width: 60, title: '款别', filterDatas: app.datas_('crm_gwei009_gweo001'),
                edit:'select'}
            , {field: 'GWEO104', width: 150, title: '订单号',event:'openOea'}
            , {field: 'GWEO003', width: 60, title: '汇率',edit:'text'}
            , {field: 'GWEO004', width: 100, title: '原币金额',edit:'text'}
            , {field: 'GWEO005', width: 100, title: '本币金额',edit:'text'}
            , {field: 'GWEO009', width: 150, title: '备注',edit:'text'}
        ]],
        callObj,
        moduleName,
        layOpenIndex,
        listData=[],
        gweoTable,
        pageVue
    ;

    var Gwei009Edit = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwei009_edit',
                _loadTplsFull:[
                    {
                        dom: 'common_open_window', tpl: 'common/_open_window'
                    }
                ]
            }, win);
        },

        //初始化表格
        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                gweoTable = table.render({
                    elem: app.getByid(doc, 'gwei009_edit_table'),
                    id: 'gwei009_edit_table',
                    cols: cols,
                    data: listData,
                    loading: true,
                    page: true,
                    height:300
                });

                table.on('tool(gwei009_edit_table)', function (obj) {
                    if (obj.event === 'openOea') {
                        if (vueData.form.GWEO006 == '' || vueData.form.GWEO006 == null) {
                            app.showWarning("请先选择客户！");
                            return;
                        }
                        if(vueData.form.GWEO002 == '' || vueData.form.GWEO002 == null){
                            app.showWarning("请先选择币种！");
                            return;
                        }
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method:'cq_gwcs_1',
                                    columnList: [
                                        {label: '订单号', prop: 'GWCSDOCNO', width: '150'},
                                        {label: '订单日期', prop: 'GWCSDOCDATE', width: '150'},
                                        {label: '客户编号', prop: 'GWCS010', width: '150'},
                                        {label: '客户名称', prop: 'GWAKL004', width: '150'},
                                        {label: '订单总额', prop: 'GWCS036_SUM', width: '150'}
                                    ],
                                    searchParam: {
                                        GWEO006:vueData.form.GWEO006,
                                        GWEO002:vueData.form.GWEO002
                                    }
                                },
                                cb: function (data) {
                                    obj.updateOfEdit({
                                        'GWEO104': data.GWCSDOCNO,
                                        'GWEO004': data.GWCS036_SUM
                                    });
                                    obj.updateOfEdit({
                                        'GWEO005': Math.round(math.mul(obj.data.GWEO003,data.GWCS036_SUM)*vueData.digit.amount) /vueData.digit.amount
                                    })
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择订单号',
                                area: ['600px', '465px']
                            }
                        });
                    }
                });

                //监听单元格编辑
                table.on('edit(gwei009_edit_table)', function (obj) {
                    if (obj.field == 'GWEO003') {
                        if (!(/^\d+(\.\d+)?$/).test(obj.value)) {
                            obj.updateOfEdit({
                                'GWEO003': 0,
                                'GWEO005': 0
                            });
                            app.showMessage('汇率不可以小于0');
                            return;
                        } else {
                            if(obj.data.GWEO004 != '' && obj.data.GWEO004 != null){
                                obj.updateOfEdit({
                                    'GWEO003': obj.value,
                                    'GWEO005': Math.round(obj.value*obj.data.GWEO004*vueData.digit.amount)/vueData.digit.amount
                                });
                            }else{
                                obj.updateOfEdit({
                                    'GWEO003': obj.value,
                                });
                            }
                        }
                    }else if(obj.field == 'GWEO004'){
                        if (!(/^\d+(\.\d+)?$/).test(obj.value)) {
                            obj.updateOfEdit({
                                'GWEO004': 0,
                                'GWEO005': 0
                            });
                            app.showMessage('原币金额不可以小于0');
                            return;
                        } else {
                            if(obj.data.GWEO003 != '' && obj.data.GWEO003 != null){
                                obj.updateOfEdit({
                                    'GWEO004': obj.value,
                                    'GWEO005': Math.round(obj.value*obj.data.GWEO003*vueData.digit.amount)/vueData.digit.amount
                                });
                            }else{
                                obj.updateOfEdit({
                                    'GWEO004': obj.value,
                                });
                            }
                        }
                    }else if(obj.field == 'GWEO005'){
                        if (!(/^\d+(\.\d+)?$/).test(obj.value)) {
                            obj.updateOfEdit({
                                'GWEO005': 0
                            });
                            app.showMessage('本币金额不可以小于0');
                            return;
                        }
                    }
                });

            });
        },

        //表格重载
        tableReload: function (listData) {
            // table.reload('gwei009_edit_table', {
            //     data: listData
            // });
            self._reloadTable({id: 'gwei009_edit_table', data: listData});
        },



        //保存import数据
        saveImportData:function(rowdata){
            var tableList=gweoTable.getDatas();
            var length = tableList.length;
            var num = 0;
            if (length != 0) {
                var obj = tableList.find(function (value, index) {
                    return index==length-1;
                });
                num=obj.GWECSEQ;
            }
            for (var i = 0; i < rowdata.length; i++) {
                num = num + 1;
                tableList.push({
                    "ROW_ID": num,
                    "GWEC014": rowdata[i]["GWCS046"],
                    "GWEC013": rowdata[i]["GWCS042"],
                    "GWEC003": rowdata[i]["OEB01"],
                    "GWEC101": rowdata[i]["OEB03"],
                    "GWECDOCDATE": rowdata[i]["OEA02"],
                    "GWEC004": rowdata[i]["OEB04"],
                    "GWCS002": rowdata[i]["OEB05"],
                    "GWCS003": rowdata[i]["OEB12"],
                    "count": rowdata[i]["count"],
                    "GWEC009": rowdata[i]["OEB12"]-rowdata[i]["count"],
                    "GWEC002": vueData.form.GWECDOCDATE,
                    "GWEC011": vueData.form.NAME_USER,
                    "GWEC012": vueData.form.NAME,
                    "GWECDOCNO": vueData.form.GWECDOCNO,
                    "GWECSEQ": num
                });
            }
            listData=tableList;
            self.tableReload(listData);
        },

        //新增
        add: function (tableDatas) {
            app.ajax({
                xurl: 'crm/gwei009/add',
                xdata: tableDatas,
                loading: true,
                toJsonStr: true,
                okmsg: '回款信息新增成功',
                cb: function (result) {
                    if (result.ok) {
                        app.pageModule("gwei009").reloadTableCurr();
                        layer.close(layOpenIndex);
                    }
                }
            })
        },

        //修改
        edit: function (tableDatas) {
            app.ajax({
                xurl: 'crm/gwei009/edit',
                xdata: tableDatas,
                loading: true,
                toJsonStr: true,
                okmsg: '出货信息修改成功',
                cb: function (result) {
                    if (result.ok) {
                        app.pageModule('gwei009').reloadTableCurr();
                        layer.close(layOpenIndex);
                    }

                }
            });
        },

        _call: function (tagData) {
            pageVue.$refs['form'].resetFields();
            moduleName = tagData.moduleName;
            layOpenIndex=tagData.layOpenIndex;
            if (tagData.isAdd) {
                vueData.isAdd = true;
                vueData.isDetail=false;
                app.ajax({
                    type:'POST',
                    dataType:'json',
                    xurl:'crm/gwei009/getAddDef',
                    cb: function (result) {
                        if(result.ok){
                            app.extendUpLow({
                                target: vueData.form,
                                res: {
                                    GWEODOCNO: result.data.data.GWEODOCNO,
                                    NAME_ORG: result.data.data.NAME_ORG,
                                    NAME_USER: result.data.data.NAME_USER,
                                    GWEO008: result.data.data.ORG_ID,
                                    GWEO007: result.data.data.USER_ID,
                                    GWEODOCDATE: new Date().Format("yyyy-MM-dd")
                                },
                                reset: true
                            });
                            app.ajax({
                                type:'POST',
                                dataType:'json',
                                xurl:'crm/gwei009/getTreeData',
                                xdata:{
                                    PARENT_ID:vueData.form.GWEO008
                                },
                                async: false,
                                cb: function (result) {
                                    if(result.ok && result.data.data.length > 0) {
                                        vueData.form.NAME_AZP = result.data.data[0].NAME;
                                        vueData.form.GWEOMLEGAL = result.data.data[0].ID;
                                        vueData.form.GWEOPLANT = result.data.data[0].BIZ_CODE;
                                        vueData.form.AZP02 = result.data.data[0].AZP02;
                                    }
                                }
                            });

                        }
                    }
                });
                listData=[];
                self.tableReload(listData);
            } else {
                vueData.isAdd = false;
                if (tagData.isDetail == true) {//查看
                    vueData.isDetail = true;//禁用按钮
                } else {//修改
                    vueData.isDetail = false;//开启按钮
                }
                callObj = tagData.obj;
                app.extendUpLow({
                    target: vueData.form,
                    res: callObj.data,
                    reset: true
                });

                // 获取汇率
                app.ajax({
                    type:'POST',
                    dataType:'json',
                    xurl:'crm/gwei009/getRate',
                    xdata:{
                        CURRENCY:vueData.form.GWEO002,
                        GWEODOCATE:vueData.form.GWEODOCDATE
                    },
                    cb: function (result) {
                        if(result.ok){
                            if(result.data.data.RATE != 'N'){
                                vueData.gweo003 = Math.round(result.data.data.RATE*vueData.digit.rate)/vueData.digit.rate
                            }
                        }
                    }
                });
                app.ajax({
                    xurl: 'crm/gwei009/getGweoData',
                    xdata: {
                        GWEODOCNO: callObj.data.GWEODOCNO
                    },
                    type:'POST',
                    dataType:'json',
                    cb: function (result) {
                        if(result.ok){
                            listData=result.data.data;
                            self.tableReload(listData);
                        }

                    }
                });
            }

        },

        _ready: function () {
            table=layui.table;
            vueData.form = app.fields_(['crm_gwei009']);
            pageVue = new Vue({
                el: '#gwei009_edit',
                data: vueData,
                methods: {
                    cancle: function () {
                        self._closeTpl();
                    },
                    save: function () {
                        var length=gweoTable.getDatas().length;
                        if(length==0){
                            app.showMessage("表格内容不能为空！");
                            return;
                        }
                        this.$refs['form'].validate(function (valid) {
                            if (valid) {
                                var list=gweoTable.getDatas();
                                var c = 0;//统计表格数据中原币金额与本币金额为空字符的个数
                                for (var i = 0; i <= list.length - 1; i++) {
                                    if (list[i]["GWEO004"] == '' || list[i]["GWEO005"] == ''
                                    || list[i]['GWEO004'] < 0 || list[i]["GWEO005"] < 0
                                    || list[i]['GWEO003'] < 0){
                                        c++;
                                    }
                                }
                                if(c==0){//无金额为空字符的数据
                                    for (var j = 0; j <= list.length - 1; j++) {
                                        list[j]["GWEOSEQ"]=(j+1);
                                    }
                                    list.addMap(vueData.form, false);
                                    if (vueData.isAdd) {
                                        self.add(list);
                                    } else {
                                        self.edit(list);
                                    }
                                }else {
                                    app.showMessage('原币金额与本币金额不允许为空！');
                                    return;
                                }

                            }
                        });
                    },
                    //归属法人开窗
                    openAzp:function(){
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method:'cq_sysorg_biz_1',
                                    columnList: [
                                        {label: 'ID', prop: 'ID', width: '150'},
                                        {label: '归属法人', prop: 'NAME', width: '150'},
                                        {label: '营运中心编号', prop: 'AZP01', width: '150',sortable:false,unshow:true},
                                        {label: '营运中心名称', prop: 'AZP02', width: '150',sortable:false,unshow:true}
                                    ]
                                },
                                cb: function (data) {
                                    vueData.form.GWEOMLEGAL = data.ID;//法人组织ID
                                    vueData.form.NAME_AZP = data.NAME;//法人组织名称
                                    vueData.form.GWEOLEGAL = data.AZP01;//营运中心编号
                                    vueData.form.GWEOPLANT = data.AZP01;//营运中心编号
                                    vueData.form.AZP02 = data.AZP02;//营运中心名称
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择归属法人',
                                area: ['600px', '465px']
                            }
                        });

                    },
                    //客户开窗
                    openGwak:function(){
                        var tableDatas = gweoTable.getDatas();
                        if (vueData.form.GWEO006 != '' && vueData.form.GWEO006 != null) {
                            if(tableDatas.length > 0){
                                app.confirm({
                                    msg: '修改客户将清空表格信息，是否确认修改？',
                                    ok: function () {
                                        listData = [];
                                        self.tableReload(listData);
                                        self._openTplFull({
                                            tag: 'common_open_window',
                                            tagData: {
                                                config: {
                                                    method:'cq_gwak001_1',
                                                    columnList: [
                                                        {label: '客户编号', prop: 'GWAK001', width: '150'},
                                                        {label: '客户名称', prop: 'GWAKL004', width: '150'}
                                                    ],
                                                    // url: 'crm/gwei009/listGwakPage',
                                                },
                                                cb: function (data) {
                                                    vueData.form.GWEO006 = data.GWAK001;
                                                    vueData.form.GWAKL004=data.GWAKL004;
                                                }
                                            },
                                            index: 0,
                                            openOpts: {
                                                title: '选择客户信息',
                                                area: ['600px', '465px']
                                            }
                                        });
                                    }
                                });
                            }else{
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'cq_gwak001_1',
                                            columnList: [
                                                {label: '客户编号', prop: 'GWAK001', width: '150'},
                                                {label: '客户名称', prop: 'GWAKL004', width: '150'}
                                            ],
                                            // url: 'crm/gwei009/listGwakPage',
                                        },
                                        cb: function (data) {
                                            vueData.form.GWEO006 = data.GWAK001;
                                            vueData.form.GWAKL004=data.GWAKL004;
                                        }
                                    },
                                    index: 0,
                                    openOpts: {
                                        title: '选择客户信息',
                                        area: ['600px', '465px']
                                    }
                                });
                            }
                        }else{
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method:'cq_gwak001_1',
                                        columnList: [
                                            {label: '客户编号', prop: 'GWAK001', width: '150'},
                                            {label: '客户名称', prop: 'GWAKL004', width: '150'}
                                        ],
                                        // url: 'crm/gwei009/listGwakPage',
                                    },
                                    cb: function (data) {
                                        vueData.form.GWEO006 = data.GWAK001;
                                        vueData.form.GWAKL004=data.GWAKL004;
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择客户信息',
                                    area: ['600px', '465px']
                                }
                            });
                        }
                    },
                    //开窗选择币种
                    openBz:function () {
                        var tableDatas = gweoTable.getDatas();
                        if (vueData.form.GWEO002 != '' && vueData.form.GWEO002 != null) {
                            if(tableDatas.length > 0){
                                app.confirm({
                                    msg: '修改币种将清空表格信息，是否确认修改？',
                                    ok: function () {
                                        listData = [];
                                        self.tableReload(listData);
                                        self._openTplFull({
                                            tag: 'common_open_window',
                                            tagData: {
                                                config: {
                                                    method:'q_gwac001',
                                                    columnList: [
                                                        {label: '币种编号', prop: 'GWAC001', width: '150'},
                                                        {label: '说明', prop: 'GWACL003', width: '150'},
                                                    ],
                                                    // url: 'base/gwei014/getGwacList',
                                                },
                                                cb: function (data) {
                                                    vueData.form.GWEO002 = data.GWAC001;
                                                    //获取汇率
                                                    app.ajax({
                                                        type:'POST',
                                                        dataType:'json',
                                                        xurl:'crm/gwei009/getRate',
                                                        xdata:{
                                                            CURRENCY:vueData.form.GWEO002,
                                                            GWEODOCDATE:vueData.form.GWEODOCDATE
                                                        },
                                                        async: false,
                                                        cb: function (result) {
                                                            if(result.ok){
                                                                if(result.data.data.RATE != 'N'){
                                                                    vueData.gweo003 = Math.round(result.data.data.RATE*vueData.digit.rate)/vueData.digit.rate
                                                                }
                                                            }
                                                        }
                                                    });
                                                }
                                            },
                                            index: 0,
                                            openOpts: {
                                                title: '选择币种信息',
                                                area: ['600px', '465px']
                                            }
                                        });
                                    }
                                });
                            }else{
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_gwac001',
                                            columnList: [
                                                {label: '币种编号', prop: 'GWAC001', width: '150'},
                                                {label: '说明', prop: 'GWACL003', width: '150'},
                                            ],
                                            // url: 'base/gwei014/getGwacList',
                                        },
                                        cb: function (data) {
                                            vueData.form.GWEO002 = data.GWAC001;
                                            //获取汇率
                                            app.ajax({
                                                type:'POST',
                                                dataType:'json',
                                                xurl:'crm/gwei009/getRate',
                                                xdata:{
                                                    CURRENCY:vueData.form.GWEO002,
                                                    GWEODOCDATE:vueData.form.GWEODOCDATE
                                                },
                                                async: false,
                                                cb: function (result) {
                                                    if(result.ok){
                                                        if(result.data.data.RATE != 'N'){
                                                            vueData.gweo003 = Math.round(result.data.data.RATE*vueData.digit.rate)/vueData.digit.rate
                                                        }
                                                    }
                                                }
                                            });
                                        }
                                    },
                                    index: 0,
                                    openOpts: {
                                        title: '选择币种信息',
                                        area: ['600px', '465px']
                                    }
                                });
                            }
                        }else{
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method:'q_gwac001',
                                        columnList: [
                                            {label: '币种编号', prop: 'GWAC001', width: '150'},
                                            {label: '说明', prop: 'GWACL003', width: '150'},
                                        ],
                                        // url: 'base/gwei014/getGwacList',
                                    },
                                    cb: function (data) {
                                        vueData.form.GWEO002 = data.GWAC001;
                                        //获取汇率
                                        app.ajax({
                                            type:'POST',
                                            dataType:'json',
                                            xurl:'crm/gwei009/getRate',
                                            xdata:{
                                                CURRENCY:vueData.form.GWEO002,
                                                GWEODOCDATE:vueData.form.GWEODOCDATE
                                            },
                                            async: false,
                                            cb: function (result) {
                                                if(result.ok){
                                                    if(result.data.data.RATE != 'N'){
                                                        vueData.gweo003 = Math.round(result.data.data.RATE*vueData.digit.rate)/vueData.digit.rate
                                                    }
                                                }
                                            }
                                        });
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择币种信息',
                                    area: ['600px', '465px']
                                }
                            });
                        }
                    },
                    //业务部门开窗
                    openOrg:function(){
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method:'q_sysorg_id',
                                    columnList: [
                                        {label: '组织编号', prop: 'ID', width: '150'},
                                        {label: '组织名称', prop: 'NAME', width: '150'}
                                    ],
                                    // url: 'crm/gwei005/listOrgPage'
                                },
                                cb: function (data) {
                                    if(vueData.form.GWEO008 != data.ID){
                                        vueData.form.GWEO008 = data.ID;
                                        vueData.form.NAME_ORG = data.NAME;
                                        //清空业务人员
                                        vueData.form.GWEO007 = '';
                                        vueData.form.NAME_USER = '';
                                    }
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择业务部门',
                                area: ['600px', '465px']
                            }
                        });
                    },
                    //业务人员开窗
                    openUser:function(){
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method:'q_sysuser_id',
                                    columnList: [
                                        {label: '销售人员ID', prop: 'ID', width: '150'},
                                        {label: '销售人员账号', prop: 'ACCOUNT', width: '150'},
                                        {label: '销售人员名称', prop: 'NAME', width: '150'}
                                    ],
                                    // url: 'crm/gwei005/listUserPage',
                                    searchParam: {
                                        ORG_ID:vueData.form.GWEO008
                                    }
                                },
                                cb: function (data) {
                                    vueData.form.GWEO007 = data.ID;
                                    vueData.form.NAME_USER = data.NAME;
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择业务人员',
                                area: ['600px', '465px']
                            }
                        });
                    },


                    //添加按钮事件
                    add:function(){
                        if(vueData.form.GWEO006 == '' || vueData.form.GWEO006 == undefined){
                            app.showMessage('请先选择客户！')
                            return;
                        }
                        if(vueData.gweo003 == 'N' ){
                            app.showMessage('该币种汇率不存在！');
                            return;
                        }
                        var tableDatas = gweoTable.getDatas();
                        var index = tableDatas.length;
                        var num = 0;
                        if (index != 0) {
                            num = tableDatas[index - 1].GWEOSEQ;
                        }
                        tableDatas.push({
                            GWEOSEQ: (num + 1),
                            GWEO001: '30',
                            GWEO104: '',
                            GWEO003: vueData.gweo003,
                            GWEO004: '',
                            GWEO005: '',
                            GWEO009: ''
                        });
                        listData=tableDatas;
                        self.tableReload(tableDatas);

                    },

                    delRows:function(){
                        var tableDatas=gweoTable.getDatas();
                        var checkStatus = table.checkStatus("gwei009_edit_table");
                        if(checkStatus.data.length>0){
                            checkStatus.data.forEach(function (item) {
                                tableDatas.removeByMap({
                                    GWEOSEQ:item.GWEOSEQ
                                })
                            });
                            var count=1;
                            tableDatas.forEach(function (item) {
                                item.GWEOSEQ=count++;
                            })
                            listData=tableDatas;
                            self.tableReload(listData);
                        }
                    }
                },

                watch: {
                    // 'form.GWEO008': function (newValue,oldValue) {
                    //     vueData.form.GWEO007='';
                    //     vueData.form.NAME_USER='';
                    // }
                },
                created: function () {
                    self._setEleIsReady('gwei009_edit');
                }
            });
            self.initTable();
        },

        getDigit:function() {
            app.ajax({
                xurl: 'admin/getDigit',
                async: true,
                cb: function (result) {
                    if(result.ok) {
                        var data = result.data.data;
                        var _digit = {
                            rate: Math.pow(10,data.rate),
                            price: Math.pow(10,data.price),
                            amount: Math.pow(10,data.amount),

                        };
                        app.extendUpLow({
                            target: vueData.digit,
                            res: _digit,
                            reset: true
                        });
                    }
                }
            });
        },

        _lazy: function () {
            this._super();
            this.getDigit();
        }
    });

    return new Gwei009Edit();

});
